﻿using System;
using System.Text.RegularExpressions;

namespace SNSGame.Util
{
    /// <summary>
    /// 字符串处理工具
    /// </summary>
    public class StringTool
    {
        
        public static string CutHtmlToText(string s, int outLength)
        {
            return CutString(HtmlToText(s), outLength, "...");
        }

        /// <summary>
        /// 截取字符串一定长度，中文按两个字符计算 。默认按...省略
        /// </summary>
        /// <param name="s"></param>
        /// <param name="outLength"></param>
        /// <returns></returns>
        public static string CutString(string s, int outLength)
        {
            return CutString(s, outLength, "...");
        }

        /// <summary>
        /// 截取字符串一定长度，中文按两个字符计算
        /// </summary>
        /// <param name="s"></param>
        /// <param name="len">加上省略字符串一共也不会超过的长度</param>
        /// <param name="omitStyle">省略符号</param>
        /// <returns></returns>
        public static string CutString(string s, int len, string omitStyle)
        {
            if (!string.IsNullOrEmpty(s))
            {
                string temp = s;
                if (Regex.Replace(temp, "[^\0-\x00ff]", "zz", RegexOptions.IgnoreCase).Length <= len)
                {
                    return temp;
                }
                for (int i = temp.Length; i >= 0; i--)
                {
                    temp = temp.Substring(0, i);
                    if (Regex.Replace(temp, "[^\0-\x00ff]", "zz", RegexOptions.IgnoreCase).Length <= (len - omitStyle.Length))
                    {
                        return (temp + omitStyle);
                    }
                }
            }
            return string.Empty;
        }

        public static string HtmlToText(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            string str = s;
            return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(str, @"\<(img)[^>]*>|<\/(img)>", "", RegexOptions.IgnoreCase), @"\<(table|tbody|tr|td|th|)[^>]*>|<\/(table|tbody|tr|td|th|)>", "", RegexOptions.IgnoreCase), @"\<(div|blockquote|fieldset|legend)[^>]*>|<\/(div|blockquote|fieldset|legend)>", "", RegexOptions.IgnoreCase), @"\<(font|i|u|h[1-9]|s)[^>]*>|<\/(font|i|u|h[1-9]|s)>", "", RegexOptions.IgnoreCase), @"\<(style|strong)[^>]*>|<\/(style|strong)>", "", RegexOptions.IgnoreCase), @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase), @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase), @"\<a[^>]*", "", RegexOptions.IgnoreCase).Replace("&nbsp;", " ");
        }


        public static string Repeat(string str, int repeats)
        {
            char[] arr = str.ToCharArray();
            char[] arrDest = new char[arr.Length * repeats];
            for (int i = 0; i < repeats; i++)
            {
                Buffer.BlockCopy(arr, 0, arrDest, (i * arr.Length) * 2, arr.Length * 2);
            }
            return new string(arrDest);
        }
    }


}